package com.iptv.grape.ui.control.ad;




import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.iptv.grape.R;
import com.iptv.grape.domain.ChannelBill;
import com.iptv.grape.util.AppLog;

import java.util.List;

public class HorizontalListViewAdapter extends BaseAdapter{
    private List<ChannelBill> channelBills;
    private Context mContext;
    private LayoutInflater mInflater;
    private int selectIndex = -1;
    InnClickListener inn = new InnClickListener();

    public HorizontalListViewAdapter(Context context,  List<ChannelBill> channelBills){
        this.mContext = context;
        this.channelBills = channelBills;
        mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//LayoutInflater.from(mContext);
    }
    @Override
    public int getCount() {
        return channelBills.size();
    }
    @Override
    public Object getItem(int position) {
        return channelBills.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AppLog.getCurLog().out("getView---------------------------------");
        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.bill_item, null);

            holder.textView = convertView.findViewById(R.id.name);
            holder.time = convertView.findViewById(R.id.time);
            holder.relativeLayout = convertView.findViewById(R.id.rel_isfucs);
            holder.linearLayout = convertView.findViewById(R.id.lin_background);
            holder.linearLayoutChange = convertView.findViewById(R.id.lin_change);
            holder.relativeLayout.setFocusable(true);
            ChannelBill channelBill = channelBills.get(position);
            holder.textView.setText(channelBill.getName());
            holder.time.setText(channelBill.getShowTimeText());
            switch (channelBill.getBillType()) {
                case "before":
                    holder.linearLayout.setBackgroundResource(R.drawable.item_red_blus);
                    break;
                case "future":
                    holder.linearLayout.setBackgroundResource(R.drawable.item_gray_blus);
                    break;
                case "now":
                    holder.linearLayout.setBackgroundResource(R.drawable.item_yellow_blus);
                    break;
            }
            holder.linearLayout.setOnClickListener(inn);
            holder.relativeLayout.setOnFocusChangeListener(inn);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }
        if(position == selectIndex){
            //AppLog.getCurLog().out("" + position + "获得焦点");
            //convertView.setSelected(true);
            holder.linearLayout.setBackgroundResource(R.drawable.item_blue_fucs);
            //holder.relativeLayout.requestFocus();
        }else{
            convertView.setSelected(false);
        }
        return convertView;
    }

    public static class ViewHolder {
        private TextView textView;
        private TextView time;
        private RelativeLayout relativeLayout;
        private LinearLayout linearLayout;
        private LinearLayout linearLayoutChange;

        public TextView getTextView() {
            return textView;
        }

        public TextView getTime() {
            return time;
        }

        public RelativeLayout getRelativeLayout() {
            return relativeLayout;
        }

        public LinearLayout getLinearLayout() {
            return linearLayout;
        }

        public LinearLayout getLinearLayoutChange() {
            return linearLayoutChange;
        }
    }

    private class InnClickListener implements View.OnClickListener,View.OnFocusChangeListener{

        public InnClickListener(){

        }
        @Override
        public void onClick(View view) {
            //LinearLayout linearLayout = (LinearLayout)view;
            AppLog.getCurLog().out("onClick");
        }

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            //LinearLayout linearLayout = (LinearLayout)view;
            AppLog.getCurLog().out("onFocusChange");
            view.setBackgroundResource(R.drawable.item_yellow_blus);
        }
    }

    public void setSelectIndex(int i){
        selectIndex = i;
    }
}
